package com.jshcbd.modules.asset.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshcbd.framework.vo.Result;
import com.jshcbd.framework.query.QueryGenerator;
import com.jshcbd.common.util.DateUtils;
import com.jshcbd.modules.asset.dto.DisposeDto;
import com.jshcbd.modules.asset.entity.DisposeEntity;
import com.jshcbd.modules.asset.service.IAssetService;
import com.jshcbd.modules.asset.service.IDisposeService;
import com.jshcbd.modules.asset.vo.AssetDisposeVo;
import com.jshcbd.modules.asset.vo.AssetVo;
import com.jshcbd.modules.asset.vo.DisposeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Api(tags="资产处置")
@RestController
@RequestMapping("/dispose")
public class DisposeController {

    @Autowired
    private IDisposeService handleService;

    @Autowired
    private IAssetService assetService;


    @ApiOperation("资产未处置列表")
    @RequestMapping(value = "/assetlist", method = RequestMethod.GET)
    public Result<IPage<AssetVo>> queryPageList(AssetVo assetVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<AssetVo>> result = new Result();
        QueryWrapper<AssetVo> queryWrapper = QueryGenerator.initQueryWrapper(assetVo, req.getParameterMap());
        queryWrapper.ne("state",3);
        queryWrapper.ge("useful_life", DateUtils.getDate());
        Page<AssetVo> page = new Page(pageNo, pageSize);
        IPage<AssetVo> pageList = assetService.pageView(page, queryWrapper);
        result.setSuccess(true);
        result.setData(pageList);
        log.info(pageList.toString());
        return result;
    }

    @ApiOperation("资产处置列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<DisposeVo>> queryPageList(DisposeVo handleVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<DisposeVo>> result = new Result();
        QueryWrapper<DisposeVo> queryWrapper = QueryGenerator.initQueryWrapper(handleVo, req.getParameterMap());
        Page<DisposeEntity> page = new Page(pageNo, pageSize);
        IPage<DisposeVo> pageList = handleService.selectPageView(page, queryWrapper);
        result.setSuccess(true);
        result.setData(pageList);
        return result;
    }




    @ApiOperation("新增处置记录")
    @PostMapping("/add")
    public Result add(@RequestBody DisposeDto disposeDto) {
        try {
            handleService.doDispose(disposeDto);
            return Result.ok("操作成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }


    @ApiOperation("资产处置记录")
    @GetMapping( value = {"/listByMainId/{assetId}","listByMainId"})
    public Result<List<AssetDisposeVo>> listByMainId(@PathVariable(required = false) String assetId, HttpServletRequest req) {
        Result<List<AssetDisposeVo>> result = new Result();
        List<AssetDisposeVo> dataList = handleService.pageViewByAssetId(assetId);
        result.setData(dataList);
        return result;
    }

}
